# -*- encoding:utf-8 -*-
# @time: 2023/4/6 20:46
# @author: Maxs_hu
from django.conf import settings
from django.http import QueryDict
from django.template import Library
from django.urls import reverse
from django.utils.safestring import mark_safe

register = Library()


def check_permission(request, current_url):
    # 1.获取当前用户的角色
    role = request.user_obj.role

    # 2.通过角色获取到所有的权限
    url_list = settings.NB_PERMISSION[role].keys()

    # 3.和页面上的url进行比对. 如果不存在. 没有权限返回False
    if current_url not in url_list:
        return False
    return True


@register.simple_tag
def add_permission(request, current_url):
    if not check_permission(request, current_url):
        return ""
    # 4.如果存在. 返回相应标签页
    url_path = reverse(current_url)  # 反向生成url
    a_str = """
    <a href="{}" class="btn btn-primary">添加 <span class="glyphicon glyphicon-plus-sign"></span> </a>
    """.format(url_path)
    return mark_safe(a_str)


@register.simple_tag
def edit_permission(request, current_url_name, *args, **kwargs):  # 这样写. 后面传递的参数都会在args或kwargs中
    if not check_permission(request, current_url_name):
        return ""

    # 处理页面跳转携带原参数问题
    path_url = reverse(current_url_name, args=args)
    param = request.GET.urlencode()  # 获取当前url携带的参数
    if param:
        new_query_dict = QueryDict(mutable=True)   # 修改成可编辑的
        new_query_dict['_filter'] = param  # 添加参数
        filter_string = new_query_dict.urlencode()
        a_str = """<a href="{}?{}" class="btn btn-primary btn-sm">编辑</a>""".format(path_url, filter_string)
        return mark_safe(a_str)

    a_str = """<a href="{}" class="btn btn-primary btn-sm">编辑</a>""".format(path_url)
    return mark_safe(a_str)
    # # 4.如果存在. 返回相应标签页
    # url_path = reverse(current_url, args=args)  # 这里多了个位置参数. 需要传递
    # a_str = """
    # <a href="{}" class="btn btn-info btn-sm">编辑</a>
    # """.format(url_path)
    # return mark_safe(a_str)


@register.simple_tag
def delete_permission(request, current_url, *args, **kwargs):
    if not check_permission(request, current_url):
        return ""

    # 4.如果存在. 返回相应标签页
    a_str = """
    <a class="btn btn-danger btn-sm deletePolicy" nid="{}">删除</a>
    """.format(kwargs['nid'])  # 通过关键字参数传递
    return mark_safe(a_str)


@register.simple_tag
def reset_permission(request, current_url, *args, **kwargs):
    if not check_permission(request, current_url):
        return ""

    # 4.如果存在. 返回相应标签页
    url_path = reverse(current_url, args=args)
    a_str = """
    <a href="{}" class="btn btn-link btn-sm">重置密码</a>
    """.format(url_path)
    return mark_safe(a_str)


@register.simple_tag
def level_delete_permission(request, current_url, *args, **kwargs):
    if not check_permission(request, current_url):
        return ""

    # 4.如果存在. 返回相应标签页
    url_path = reverse(current_url, args=args)
    a_str = """
    <a href="{}" class="btn btn-danger btn-sm">删除</a>
    """.format(url_path)
    return mark_safe(a_str)


@register.filter
def table_permission(request, current_url):
    # 1.获取当前角色
    role = request.user_obj.role
    # 2.根据角色找出所有权限url
    url_list = settings.NB_PERMISSION[role]

    # 3.判断如果表格中table_url在权限url_list中则显示table
    status = False
    table_url = current_url.split(',')
    for url in table_url:
        if url in url_list:
            status = True
    return status
